package UILayer;
import java.util.Scanner;
import CtrlLayer.*;
import ModelLayer.SaleItem;
import ModelLayer.RentItem;

public class ItemUI
{
	private ItemCtrl IC;
	private SaleItem mysaleitem;
	private RentItem myrentitem;
    
    public ItemUI()
    {
    	IC = new ItemCtrl();
    	
    }
    
    public void ItemMenu()
    {   
    	boolean exit = false;
        while (!exit) //! means while exit not is true (that is: false)
        {
        	int choise = writeItemMenu();
        	switch(choise)
        	{
        		case 1: addSaleItem(); break;
        		case 2: getSaleItemUI(); break;
        		case 3: addRentItem(); break;
        		case 4: getRentItem(); break;
        		case 99: exit = true; break;
        	}//end switch
        }//end while
    }
    
    private int writeItemMenu()
    {
        //creates an object keyboard to read data from the keyboard
    	Scanner keyboard = new Scanner(System.in);
    	System.out.println(" *** Lager Menu ***");
    	System.out.println(" (1) Opret varer salg ");
    	System.out.println(" (2) Find varer salg ");
    	System.out.println(" (3) Opret varer udlejning ");
    	System.out.println(" (4) Find varer udlejning ");
    	System.out.println(" (99) Tilbage");
    	System.out.print("\n Make your choise: ");
    	int choise = keyboard.nextInt();
    	return choise;
    }
    
    public void addSaleItem()
    {
    	int itemnr = inputintnumber("Vare nr ");
    	String description = inputtext("Beskrivelse ");
    	double price = inputnumber("Pris ");
    	int inStock = inputintnumber("Antal ");
    	String location = inputtext("Lokation ");
    	String barCode = inputtext("Stregkode ");
    	int minAmount = inputintnumber("min antal vare ");
    	int maxAmount = inputintnumber("max antal vare ");
    	IC.createSaleItem(itemnr, description, price, inStock, location, barCode, minAmount, maxAmount);
    }
    
    public void addRentItem()
    {
    	int itemnr = inputintnumber("Vare nr ");
    	String description = inputtext("Beskrivelse ");
    	double price = inputnumber("Pris ");
    	int inStock  = inputintnumber("Antal paa lager ");
    	String location = inputtext("Lokation ");
    	String serialnr  = inputtext("Serie nr ");
    	boolean rented = false;
    	IC.createRentItem(itemnr, description, price, inStock, location, serialnr, rented);
    }
    
    public void getSaleItemUI()
    {
    	int itemnr = inputintnumber("Varenr ");
    	mysaleitem = getSaleItem(itemnr);
    	System.out.println("Varenr     : " + mysaleitem.getItemnr() );
    	System.out.println("Beskrivelse: " + mysaleitem.getDescription() );
    	System.out.println("Pris       : " + mysaleitem.getPrice() );
    	System.out.println("Paa lager  : " + mysaleitem.getInStock() );
    	System.out.println("Lokation   : " + mysaleitem.getLocation() );
    	System.out.println("Stregkode  : " + mysaleitem.getBarCode() );
    	System.out.println("Min lager  : " + mysaleitem.getMinAmount() );
    	System.out.println("Max lager  : " + mysaleitem.getMaxAmount() );
    }
    
    public SaleItem getSaleItem(int itemnr)
    {
    	return IC.searchSaleItem(itemnr);
    }
    
    public void getRentItem()
    {
    	int itemnr = inputintnumber("Varenr ");
    	myrentitem = IC.searchRentItem(itemnr);
    	System.out.println( myrentitem.getItemnr() );
    	System.out.println( myrentitem.getDescription() );
    	System.out.println( myrentitem.getPrice() );
    	System.out.println( myrentitem.getInStock() );
    	System.out.println( myrentitem.getLocation() );
    	System.out.println( myrentitem.getSerialnr() );
    	System.out.println( myrentitem.getRented() );
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return str Return the input from the keyboard 
     */
    private String inputtext(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        return str;
    }
    
    /**
     * Print a text and takes input from keyboard
     * @param text Print text
     * @return no Return the input from the keyboard as a long
     */
    private double inputnumber(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        double no = Double.parseDouble(str.trim());  // convert String 2 long
        return no;
    }
    
    private int inputintnumber(String text)
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.print(text + ": ");
        String str = keyboard.nextLine();
        int no = Integer.parseInt(str.trim());  // convert String 2 long
        return no;
    }
    
}